#include <stdlib.h>
#include "nvs_flash.h"
#include "app_lcd.h"
#include "app_data.h"
#include "app_ble.h"
#include "bsp_backlight.h"
#include "bsp_buzz.h"
#include "bsp_can.h"
#include "bsp_lcd.h"
#include "bsp_motor.h"
#include "bsp_sleep.h"
#include "bsp_ota.h"
#include "test_lcd.h"

#include "lib_iic.h"

app_data_t app_data;
volatile uint32_t app_tick;

static void app_tick_task(void *arg)
{
	app_tick++;
}

void app_nvs_init(void)
{
	esp_err_t err = nvs_flash_init();
	if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
	{
		ESP_ERROR_CHECK(nvs_flash_erase());
		err = nvs_flash_init();
	}
	ESP_ERROR_CHECK(err);
}

void app_timer_init(void)
{
	app_tick = 0;

	const esp_timer_create_args_t periodic_timer_args = {
		.callback = &app_tick_task,
		.name = "periodic_gui"};
	esp_timer_handle_t periodic_timer;
	ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
	ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 1000));
}

void app_init_var(void)
{
	/** 变量初始化 **/
	app_data.light = 50;
	app_data.light_mode = MANUAL;
	app_data.backlight_duty = 0;

	app_data.ble_enable = 0;
	app_data.update_cmd = 0;
	app_data.phone = 0;
	app_data.standard_angle = 0;
	app_data.can_type = 0;
	app_data.sleep_flag = 0;
	app_data.engine_status = 0;
	app_data.engine_start_time = 0;
	app_data.engine_stop_time = 0;

	app_data.nav.enable = 0;
	app_data.nav.speed = 0;
	app_data.nav.speed_limit = 120;
	app_data.nav.camera = 0;
	app_data.nav.next_dir = 0;
	app_data.nav.next_dist = 0;

	app_data.obd.enable = 1;
	app_data.obd.speed = 123;
	app_data.obd.water = 60;
	app_data.obd.rotation = 400;
	app_data.obd.rotation_enable = 0;
	app_data.obd.gas = 9;
	app_data.obd.voltage = 12;
	app_data.obd.last_rotation_enable_time = 0;
	app_data.obd.last_rotation_disable_time = 0;
	app_data.obd.last_enable_time = 0;
	app_data.obd.last_disable_time = 0;

	app_data.alarm[OVER_SPEED].alarm = 0;
	app_data.alarm[OVER_SPEED].area[0] = AREA_BRACKETS_LEFT;
	app_data.alarm[OVER_SPEED].area[1] = AREA_BRACKETS_RIGHT;
	app_data.alarm[OVER_SPEED].beep_done = 0;
	app_data.alarm[HIGH_TEMP].alarm = 0;
	app_data.alarm[HIGH_TEMP].area[0] = AREA_WATER_SIGN;
	app_data.alarm[HIGH_TEMP].beep_done = 0;
	app_data.alarm[LOW_GAS].alarm = 0;
	app_data.alarm[LOW_GAS].area[0] = AREA_GAS_ADD;
	app_data.alarm[LOW_GAS].beep_done = 0;
	app_data.alarm[AREA_TIRED].alarm = 0;
	app_data.alarm[AREA_TIRED].area[0] = AREA_TIRED;
	app_data.alarm[AREA_TIRED].beep_done = 0;
}

void app_main(void)
{
	/** 上电初始化 顺序不能乱**/
	app_nvs_init();
	bsp_ota_wakeup();
	app_init_var();
	bsp_adc_init();
	bsp_lcd_init();
	bsp_lcd_alloff();
	bsp_backlight_init();
	// bsp_backlight_set_duty(1.0);
	bsp_backlight_start();
	bsp_sleep_wakeup_info();
	app_timer_init();
	bsp_buzz_init();
	bsp_sleep_init();
	vTaskDelay(1000 / portTICK_RATE_MS);

	/** 创建线程 **/
	// xTaskCreate(test_lcd_task, "test_lcd_task", 2048, NULL, 1, NULL);
	// xTaskCreate(i2c_test_task, "i2c_test_task", 2048, NULL, 1, NULL);
	srand(0);
	xTaskCreate(app_lcd_task, "app_lcd_task", 2048, NULL, 0, NULL);				// 开启液晶显示控制线程
	xTaskCreate(app_data_task, "app_data_task", 2048, NULL, 1, NULL);			// 开启数据处理线程
	xTaskCreate(app_ble_task, "app_ble_task", 4096, NULL, 2, NULL);				// 开启蓝牙收发线程
	xTaskCreate(bsp_can_task, "bsp_can_task", 2048, NULL, 3, NULL);				// 开启CAN收发线程
	xTaskCreate(motor_control_task, "motor_control_task", 2048, NULL, 3, NULL); // 开启反光镜控制线程

	while (1)
	{
		// 按键睡眠
		if (app_data.sleep_flag)
		{
			bsp_sleep_start();
		}
		// char buffer[2048];
		// vTaskList((char *)&buffer);
		// printf("task_name   task_state  priority   stack  tasK_num\n");
		// printf("%s\n", buffer);

		// app_data.obd.speed = rand() % 200;
		// app_data.obd.water = rand() % 100;
		// app_data.obd.gas = rand() % 100;
		vTaskDelay(1000 / portTICK_PERIOD_MS);
	}
}